"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 功能
Case Name   : postgresql.conf 手动配置错误
Create At   : 2023/04/19
Owner       : opentestcase043
Description :
        1.查询集群状态
        2.将配置文件中的内容全部注释
        3.重启集群
        4.将配置文件中注释的内容恢复
        5.重启集群
        6.查看集群状态
        7.修改配置文件中的listen_addresses和local_bind_address
        8.重启集群
        9.恢复配置文件中的连接地址
        10.重启集群
        11.查看集群状态
        12.修改配置文件中关于ssl的配置信息
        13.重启集群
        14.恢复配置文件中修改的信息
        15.清理环境
Expect      :
        1.集群状态正常
        2.成功
        3.重启集群失败
        4.成功
        5.重启集群成功
        6.集群状态正常
        7.修改成功
        8.重启集群失败
        9.恢复成功
        10.重启集群成功
        11.集群状态正常
        12.修改成功
        13.重启集群失败
        14.恢复成功
        15.清理环境成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

LOG = Logger()
Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class ShareStorage(unittest.TestCase):

    def setUp(self):
        LOG.info('-----this is setup-----')
        LOG.info(f'-----{os.path.basename(__file__)} start-----')
        self.con = Constant()
        self.pri_node = Node('PrimaryDbUser')

    def test_share_storage(self):
        text = '-----step1:查询集群状态; expect:集群状态正常-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('query', '-Cv')
        LOG.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败:' + text)
        res = status.splitlines()[-1].split('|')[0].split('6001')[1]
        self.assertIn('P Primary Normal', res, '执行失败:' + text)

        text = '-----step2:将配置文件中的内容全部注释; expect:成功-----'
        LOG.info(text)
        cmd = f'mv {macro.DB_INSTANCE_PATH}/postgresql.conf ' \
              f'{macro.DB_INSTANCE_PATH}/postgresql.conf_bak'
        LOG.info(cmd)
        status = self.pri_node.sh(cmd).result()
        self.assertEqual('', status, '执行失败:' + text)
        cmd = f"""awk '{{{{print "#"$0}}}}' \
            {macro.DB_INSTANCE_PATH}/postgresql.conf_bak \
            > {macro.DB_INSTANCE_PATH}/postgresql.conf"""
        LOG.info(cmd)
        status = self.pri_node.sh(cmd).result()
        self.assertEqual('', status, '执行失败:' + text)

        text = '-----step3:重启集群; expect:重启集群失败-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('stop')
        LOG.info(status)
        self.assertIn(self.con.cm_stop_success_msg, status, '执行失败:' + text)
        status = Primary_SH.exec_cm_ctl('start', '-t 60')
        LOG.info(status)
        self.assertIn(self.con.cm_start_fail_msg, status, '执行失败:' + text)

        text = '-----step4:将配置文件中注释的内容恢复; expect:成功-----'
        LOG.info(text)
        cmd = f'rm -rf {macro.DB_INSTANCE_PATH}/postgresql.conf'
        LOG.info(cmd)
        status = self.pri_node.sh(cmd).result()
        self.assertEqual('', status, '执行失败:' + text)
        cmd = f'cp -p {macro.DB_INSTANCE_PATH}/postgresql.conf_bak ' \
              f'{macro.DB_INSTANCE_PATH}/postgresql.conf'
        LOG.info(cmd)
        status = self.pri_node.sh(cmd).result()
        self.assertEqual('', status, '执行失败:' + text)

        text = '-----step5:重启集群; expect:重启集群成功-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('stop')
        LOG.info(status)
        self.assertIn(self.con.cm_stop_success_msg, status, '执行失败:' + text)
        status = Primary_SH.exec_cm_ctl('start', '-t 60')
        LOG.info(status)
        self.assertIn(self.con.cm_start_success_msg, status, '执行失败:' + text)

        text = '-----step6:查看集群状态; expect:集群状态正常-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('query', '-Cv')
        LOG.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败:' + text)
        status = Primary_SH.exec_cm_ctl('switchover', '-a')
        LOG.info(status)
        self.assertIn(self.con.cm_switchover_success_msg, status,
                      '执行失败:' + text)

        text = '-----step7:修改配置文件中的listen_addresses和local_bind_address; '\
               'expect:修改成功-----'
        LOG.info(text)
        hostname = self.pri_node.sh('hostname').result()
        status = Primary_SH.execute_gsguc('set',
                                          self.con.GSGUC_SUCCESS_MSG,
                                          "listen_addresses="
                                          "'localhost,4.1.8.3'",
                                          f'{hostname}')
        LOG.info(status)
        self.assertTrue(status)
        status = Primary_SH.execute_gsguc('set',
                                          self.con.GSGUC_SUCCESS_MSG,
                                          "local_bind_address='4.1.8.3'",
                                          f'{hostname}')
        LOG.info(status)
        self.assertTrue(status)

        text = '-----step8:重启集群; expect:重启集群失败-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('stop')
        LOG.info(status)
        self.assertIn(self.con.cm_stop_success_msg, status, '执行失败:' + text)
        status = Primary_SH.exec_cm_ctl('start', '-t 60')
        LOG.info(status)
        self.assertIn(self.con.cm_start_fail_msg, status, '执行失败:' + text)

        text = '-----step9:恢复配置文件中的连接地址; expect:恢复成功-----'
        LOG.info(text)
        cmd = f'rm -rf {macro.DB_INSTANCE_PATH}/postgresql.conf'
        LOG.info(cmd)
        status = self.pri_node.sh(cmd).result()
        self.assertEqual('', status, '执行失败:' + text)
        cmd = f'cp -p {macro.DB_INSTANCE_PATH}/postgresql.conf_bak ' \
              f'{macro.DB_INSTANCE_PATH}/postgresql.conf'
        LOG.info(cmd)
        status = self.pri_node.sh(cmd).result()
        self.assertEqual('', status, '执行失败:' + text)

        text = '-----step10:重启集群; expect:重启集群成功-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('stop')
        LOG.info(status)
        self.assertIn(self.con.cm_stop_success_msg, status, '执行失败:' + text)
        status = Primary_SH.exec_cm_ctl('start', '-t 60')
        LOG.info(status)
        self.assertIn(self.con.cm_start_success_msg, status, '执行失败:' + text)

        text = '-----step11:查看集群状态; expect:集群状态正常-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('query', '-Cv')
        LOG.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败:' + text)
        status = Primary_SH.exec_cm_ctl('switchover', '-a')
        LOG.info(status)
        self.assertIn(self.con.cm_switchover_success_msg, status,
                      '执行失败:' + text)

        text = '-----step12:修改配置文件中关于ssl的配置信息; expect:修改成功-----'
        LOG.info(text)
        status = Primary_SH.execute_gsguc('set',
                                          self.con.GSGUC_SUCCESS_MSG,
                                          "ssl_cert_file='server.crt1'",
                                          f'{hostname}')
        LOG.info(status)
        self.assertTrue(status)
        status = Primary_SH.execute_gsguc('set', self.con.GSGUC_SUCCESS_MSG,
                                          "ssl_key_file='server.key1'",
                                          f'{hostname}')
        self.assertTrue(status)
        status = Primary_SH.execute_gsguc('set', self.con.GSGUC_SUCCESS_MSG,
                                          "ssl_ca_file='cacert.pem1'",
                                          f'{hostname}')
        LOG.info(status)
        self.assertTrue(status)

        text = '-----step13:重启集群; expect:重启集群失败-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('stop')
        LOG.info(status)
        self.assertIn(self.con.cm_stop_success_msg, status, '执行失败:' + text)
        status = Primary_SH.exec_cm_ctl('start', '-t 60')
        LOG.info(status)
        self.assertIn(self.con.cm_start_fail_msg, status, '执行失败:' + text)

        text = '-----step14:恢复配置文件中修改的信息; expect:恢复成功-----'
        LOG.info(text)
        cmd = f'rm -rf {macro.DB_INSTANCE_PATH}/postgresql.conf'
        status = self.pri_node.sh(cmd).result()
        LOG.info(status)
        self.assertEqual('', status, '执行失败:' + text)
        cmd = f'cp -p {macro.DB_INSTANCE_PATH}/postgresql.conf_bak ' \
              f'{macro.DB_INSTANCE_PATH}/postgresql.conf'
        status = self.pri_node.sh(cmd).result()
        LOG.info(status)
        self.assertEqual('', status, '执行失败:' + text)

    def tearDown(self):
        text = '-----this is tearDown-----'
        LOG.info(text)
        text = '-----step15:清理环境; expect:清理环境成功-----'
        LOG.info(text)
        cmd = f'rm -rf {macro.DB_INSTANCE_PATH}/postgresql.conf'
        status1 = self.pri_node.sh(cmd).result()
        cmd = f'cp -p {macro.DB_INSTANCE_PATH}/postgresql.conf_bak ' \
              f'{macro.DB_INSTANCE_PATH}/postgresql.conf'
        status2 = self.pri_node.sh(cmd).result()
        status3 = Primary_SH.exec_cm_ctl('stop')
        status4 = Primary_SH.exec_cm_ctl('start', '-t 60')
        status5 = Primary_SH.exec_cm_ctl('switchover', '-a')
        LOG.info(status1)
        LOG.info(status2)
        LOG.info(status3)
        LOG.info(status4)
        LOG.info(status5)
        self.assertEqual('', status1, '执行失败:' + text)
        self.assertEqual('', status2, '执行失败:' + text)
        self.assertIn(self.con.cm_stop_success_msg, status3, '执行失败:' + text)
        self.assertIn(self.con.cm_start_success_msg, status4, '执行失败:' + text)
        self.assertIn(self.con.cm_switchover_success_msg, status5,
                      '执行失败:' + text)
        LOG.info(f'-----{os.path.basename(__file__)} end-----')
